package com.sxpi.zixun.service.questionscomment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.zixun.bo.QuestionsCommentBo;
import com.sxpi.zixun.common.enums.ResultStatusEnum;
import com.sxpi.zixun.common.ex.PugValidatorException;
import com.sxpi.zixun.common.utils.fn.asserts.Vsserts;
import com.sxpi.zixun.mapper.QuestionsCommentMapper;
import com.sxpi.zixun.pojo.Questions;
import com.sxpi.zixun.pojo.QuestionsComment;
import com.sxpi.zixun.vo.QuestionsCommentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 董伟豪
 * @Date: 2022/9/17 11:00
 */
@Service
@Slf4j
public class IQuestionsCommentServiceImpl extends ServiceImpl<QuestionsCommentMapper, QuestionsComment> implements IQuestionsCommentService {

    /**
     * 查询文章评论列表信息
     * @return
     */
    @Override
    public List<QuestionsCommentBo> findQuestionsCommentList() {
        return null;
    }

    /**
     * 查询文章评论列表信息并分页
     * @param questionscommentVo
     * @return
     */
    @Override
    public IPage<QuestionsCommentBo> findQuestionsCommentPage(QuestionsCommentVo questionscommentVo) {
        Page<QuestionsComment> questionsPage = new Page<>(questionscommentVo.getPageNo(), questionscommentVo.getPageSize());
        LambdaQueryWrapper<QuestionsComment> questionsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 排除字段
        questionsLambdaQueryWrapper.select(QuestionsComment.class,colum-> !colum.getColumn().equals("description"));
        // 根据关键词搜索信息
        questionsLambdaQueryWrapper.and(Vsserts.isNotEmpty(questionscommentVo.getKeyword()), wrapper -> wrapper
                .like(QuestionsComment::getNickname,questionscommentVo.getKeyword())
                .or()
                // 根据关键词搜索信息
                .like(QuestionsComment::getQuestiontitle,questionscommentVo.getKeyword())
                .or()
                // 根据关键词搜索信息
                .like(QuestionsComment::getQuestioncatename,questionscommentVo.getKeyword())
                .or()
                // 根据关键词搜索信息
                .like(QuestionsComment::getReplyusernickname,questionscommentVo.getKeyword()));
        // 根据时间排降序
        questionsLambdaQueryWrapper.orderByDesc(QuestionsComment::getCreateTime);
        // 查询分页返回
        IPage<QuestionsComment> results = this.page(questionsPage,questionsLambdaQueryWrapper);
        return tranferPageBo(results,QuestionsCommentBo.class);
    }

    /**
     * 根据文章评论id查询明细信息
     * @param id
     * @return
     */
    @Override
    public QuestionsCommentBo getQuestionsCommentById(Long id) {
        QuestionsComment questionsComment = this.getById(id);
        return tranferBo(questionsComment,QuestionsCommentBo.class);
    }

    /**
     * 保存和修改文章评论
     * @param questionscomment
     * @return
     */
    @Override
    public QuestionsCommentBo saveUpdateQuestionsComment(QuestionsComment questionscomment) {
        boolean flag = this.saveOrUpdate(questionscomment);
        return flag ? tranferBo(questionscomment,QuestionsCommentBo.class)  : null;
    }

    /**
     * 根据文章评论id删除文章评论
     * @param id
     * @return
     */
    @Override
    public int deleteQuestionsCommentById(Long id) {
        QuestionsComment adminRole = this.getById(id);
        if (adminRole != null) {
            QuestionsComment questionscomment2 = new QuestionsComment();
            questionscomment2.setId(id);
            boolean b = this.updateById(questionscomment2);
            return 1;
        }
        return 0;
    }

    /**
     * 根据文章评论ids批量删除文章评论
     * @param batchIds
     * @return
     */
    @Override
    public boolean delBatchQuestionsComment(String batchIds) {
        try {
            // 1 : 把数据分割
            String[] strings = batchIds.split(",");
            // 2: 组装成一个List<QuestionsComment>
            List<QuestionsComment> questionscommentList = Arrays.stream(strings).map(idstr -> {
                QuestionsComment questionscomment = new QuestionsComment();
                questionscomment.setId(new Long(idstr));
                return questionscomment;
            }).collect(Collectors.toList());
            // 3: 批量删除
            return this.updateBatchById(questionscommentList);
        } catch (Exception ex) {
            throw new PugValidatorException(ResultStatusEnum.SERVER_DB_ERROR);
        }
    }
}
